package com.personal.summaryofproject.utils.poi;

import com.personal.summaryofproject.constant.ConstantParams;
import com.personal.summaryofproject.utils.poi.bean.FileDataBean;
import com.personal.summaryofproject.utils.poi.bean.SheetDataBean;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;

/**
 * @Classname PoiUtils
 * @Description Excel 文件读写工具
 * @Date 2019/7/11 18:13
 * @Created by wenjun.dai
 */
@Slf4j
public class PoiUtils {

    /**
     * 获取文件所有数据
     *
     * @param fileEncode
     * @param fileName
     * @return
     */
    public static FileDataBean getFileDataBean(String fileEncode, String fileName, List<String> insteadColumnList) {
        File file = getFile(fileEncode, fileName);
        return GetExcelFileDataUtil.getFileDataAll(file, insteadColumnList);
    }

    /**
     * 获取指定索引的 sheet 页数据
     *
     * @param fileEncode
     * @param fileName
     * @param index
     * @param insteadColumnList
     * @return
     */
    public static SheetDataBean getSheetDataBean(String fileEncode, String fileName, Integer index,
                                                 List<String> insteadColumnList) {
        File file = getFile(fileEncode, fileName);
        return GetExcelFileDataUtil.getSheetDataByIndex(file, index, insteadColumnList);
    }

    public static SheetDataBean getSheetDataBean(File file, Integer index,
                                                 List<String> insteadColumnList) {
        return GetExcelFileDataUtil.getSheetDataByIndex(file, index, insteadColumnList);
    }


    /**
     * 获取指定名称的 sheet 页数据
     *
     * @param fileEncode
     * @param fileName
     * @param sheetName
     * @return
     */
    public SheetDataBean getSheetDataBean(String fileEncode, String fileName, String sheetName,
                                          List<String> insteadColumnList) {
        File file = getFile(fileEncode, fileName);
        return GetExcelFileDataUtil.getSheetDataBySheetName(file, sheetName, insteadColumnList);
    }


    /**
     * 将文件 Base64 字符串转为文件
     *
     * @param fileEncode
     * @param fileName
     * @return
     */
    public static File getFile(String fileEncode, String fileName) {
        if (!fileName.contains(".")) {
            fileName = fileName + ".xls";
        }
        String filePath = ConstantParams.TMP_DIR_IMPORT + "/" + fileName;
        FileOutputStream out = null;
        try {
            byte[] buffer = new BASE64Decoder().decodeBuffer(fileEncode);
            File temp = new File(ConstantParams.TMP_DIR_IMPORT);
            if (!temp.exists()) {
                temp.mkdirs();
            }
            out = new FileOutputStream(filePath);
            out.write(buffer);
            out.close();
        } catch (IOException e) {
            log.error("文件转换异常!", e);
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    log.error("文件流关闭异常!", e);
                }
            }
        }
        return new File(filePath);
    }



    /**
     * 文件转 base64
     *
     * @param filePath
     * @return
     */
    public static String encryptToBase64(String filePath) {
        if (filePath == null) {
            return null;
        }
        try {
            byte[] b = Files.readAllBytes(Paths.get(filePath));
            return Base64.getEncoder().encodeToString(b);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}
